(function () {
    // var LODOP; // 声明为全局变量
    var list_indes = 20;
    var table_left = 5;
    var tab_style = "";
    var tab_begin = "";
    var tab_end = "";
    // var thead = "";
    // var tfooter = "";
    var printhtml = "";
    var printhtml_temp = "";
    var feeprintLeft;
    var feeprintFontTitleFontWidth;
    var TABLE_Width = 160;// table宽度
    var TABLE_HEAD = 25;// 标题高度
    var TABLE_HEIGHT = 0;
    var TABLE_TR = 35;// table行高度
    var dataLenth = 0;
    var dataTable = "";
    // var title = '护理佳纸业产品合格证';//票据温馨提示

    tab_style = "<style>td,th{border:1px solid #000;}</style>";
    tab_begin = "<table border='0'  cellpadding='5' style='border:solid 0 #000000;border-collapse:collapse; font-size:16px;font-weight: bold; flont-family:微软雅黑;text-align:center; width:330px '>";
    thead = "<thead></thead>";
    tfooter = "<tfoot></tfoot>";
    tab_end = "</table>";

    function WarnPrint(data) {
        LODOP.PRINT_INIT(data.title);
        CreatePage(data);
        LODOP.SET_PRINT_PAGESIZE(1, 1000, 1500, "");//这里3表示纵向打印且纸高“按内容的高度”；1000表示纸宽100mm；
        // LODOP.PRINT_DESIGN();
        LODOP.PREVIEW();
    }

    function CreatePage(data) {
        pt = new PrintTemp(data);
        printhtml_temp = printhtml + pt.render();
        printhtml += pt.render();
        dataTable = tab_style + tab_begin + "<tbody>" + printhtml
            + "</tbody>" + tab_end;
        initTable(data);
        LODOP.NewPage();
        LODOP.ADD_PRINT_TABLE(list_indes + 30, table_left, TABLE_Width, TABLE_HEIGHT, dataTable);
        printhtml = "";
        feeprintLeft = (1245 - feeprintFontTitleFontWidth) / 2 + 30;
        if ((feeprintLeft + feeprintFontTitleFontWidth) > 1145) {
            feeprintLeft = 1245 - feeprintFontTitleFontWidth + 30;
        }
//        LODOP.ADD_PRINT_LINE(6, 2, 6, 345, 0, 1);
        // LODOP.ADD_PRINT_TEXT(10, table_left, 200, 18, title);
        LODOP.ADD_PRINT_TEXT(15, 49, feeprintFontTitleFontWidth, 15, data.title);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 20);
        LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
        LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);//内容在打印项中水平居中
        LODOP.SET_PRINT_STYLEA(0, "Horient", 2);//打印项在纸张位置水平居中

        // LODOP.ADD_PRINT_LINE(TABLE_HEIGHT+ list_indes, 2, TABLE_HEIGHT+ list_indes, 345, 0, 1);

        LODOP.ADD_PRINT_TEXT( 18+TABLE_HEIGHT+ list_indes, 15, 200, 15, "开卷方向：");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 15);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);

        LODOP.ADD_PRINT_IMAGE(10+TABLE_HEIGHT + list_indes+5, table_left + 100, 240, 25, "<img border='0' src='"+data.opendirection+"' width='240' height='25'/>");

        LODOP.ADD_PRINT_BARCODE(50 + TABLE_HEIGHT + list_indes, table_left + 5, 120, 120, "QRCode", data.qrcode);

        LODOP.ADD_PRINT_TEXT(60 + TABLE_HEIGHT + list_indes, table_left + 130, 300, 15,"地址："+data.address);
        LODOP.SET_PRINT_STYLEA(0, "FontColor", "#000000");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);

        LODOP.ADD_PRINT_TEXT(80 + TABLE_HEIGHT + list_indes, table_left + 130, 300, 15, "传真："+data.tax);
        LODOP.SET_PRINT_STYLEA(0, "FontColor", "#000000");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);

        LODOP.ADD_PRINT_TEXT(100 + TABLE_HEIGHT + list_indes, table_left + 130, 300, 15, "电话："+data.phone);
        LODOP.SET_PRINT_STYLEA(0, "FontColor", "#000000");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);

        LODOP.ADD_PRINT_TEXT(120 + TABLE_HEIGHT + list_indes, table_left + 130, 300, 15, "官方网址："+data.web);
        LODOP.SET_PRINT_STYLEA(0, "FontColor", "#000000");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);

        LODOP.ADD_PRINT_TEXT(145 + TABLE_HEIGHT + list_indes, table_left + 130, 300, 15, data.code);
        LODOP.SET_PRINT_STYLEA(0, "FontColor", "#000000");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 15);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    }

    function initTable(data) {
        var fontlength = parseInt(data.title.length) + 4;
        var fontwidth = 7.381 * fontlength;
        feeprintFontTitleFontWidth = fontwidth * 3.057;
        TABLE_HEIGHT = TABLE_TR * 10;
    }

    var PrintTemp = function (data) {
        var me = this;
        var ftl = "";
        var tr_rows_tfoot = "";
        var tr_rows = "";
        var bg_rows = "";

        tr_rows += "<tr><td align='left' width='100'>产品代码</td><td>" + data.cpdm + "</td></tr>"
            + "<tr><td align='left' width='100'>产品名称</td><td>" + data.cpmc + "</td></tr>"
            + "<tr><td align='left' width='100'>规格型号g/m²*mm*层</td><td>" + data.ggxh + "</td></tr>"
            + "<tr><td align='left' width='100'>接头(个)</td><td>" + data.jietou + "</td></tr>"
            + "<tr><td align='left' width='100'>直径(mm)</td><td>" + data.zj + "</td></tr>"
            + "<tr><td align='left' width='100'>班组</td><td>" + data.bz + "</td></tr>"
            + "<tr><td align='left' width='100'>机台</td><td>" + data.jt + "</td></tr>"
            + "<tr><td align='left' width='100'>生产日期</td><td>" + data.date + "</td></tr>"
            + "<tr><td align='left' width='100'>净重 Kg</td><td>" + data.amount + "</td></tr>"
            + "<tr><td align='left' width='100'>检验员</td><td>" + ""+ "</td></tr>";

        bg_rows = tr_rows + tr_rows_tfoot;
        ftl = bg_rows;
        me.render = function () {
            var render = template.compile(ftl);
            template.helper('getVal', function (v) {
                if (v) {
                    return v;
                } else {
                    return "-";
                }
            });
            template.helper('getSize', function (v) {
                if (v.length > 15) {
                    return 10;
                } else {
                    return 13;
                }
            });
            return render({
                bills: data.items
            });
        };
    }
    window.WarnPrint = WarnPrint;
})();